package com.example.demo.controller;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Controller
public class QueryController {

    @RequestMapping("/query")
    public String toQuery(){
        return "query";
    }

    @PostMapping("/getFileList")
    @ResponseBody
    public Map<String,Object> getFileDataList(){
        Map<String,Object> resultMap = new HashMap<>();
        List<Map<String,Object>> dataList = new LinkedList<>();
        SimpleDateFormat sdf=new SimpleDateFormat("HH:mm");//获取修改时间
        File rootFile = new File(FileConfig.uploadpath);
        if(rootFile.exists()){
            File[] files = rootFile.listFiles();
            for(int i=0;i< files.length;i++){
                Map<String,Object> fileMap = new HashMap<>();
                if(files[i].isFile()){
                    fileMap.put("name",files[i].getName());
                    fileMap.put("time",sdf.format(files[i].lastModified()));
                    fileMap.put("size",files[i].length()/1024+1+"KB");
                    dataList.add(fileMap);
                }else{
                    fileMap.put("name",files[i].getName());
                    fileMap.put("time",sdf.format(files[i].lastModified()));
                    fileMap.put("size",files[i].length()/1024+1+"KB");
                }
            }
        }
        resultMap.put("code","0");
        resultMap.put("msg","");
        resultMap.put("count",dataList.size());
        resultMap.put("data",dataList);
        return resultMap;
    }
}
